/**
 * Created by dukes on 2015/12/22.
 */
class VipPanel extends eui.Component{
    private curPage:number = 0;
    private maxPage:number = -1;
    public constructor(){
        super();
        this.skinName = "skins.scene.VipPanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }

    public updateDisplay(){
        this.updatePageDisplay();

    }
    private curConfig:VipConfig;
    private updatePageDisplay(){
        this.maxPage =  ActivityMgr.getInstance().vipConfigs.length;
        this.img_right.visible = this.curPage < this.maxPage-1;
        this.img_left.visible = this.curPage > 0;
        this.curConfig = ActivityMgr.getInstance().vipConfigs[this.curPage];
        this.lab_0.text = ""+this.curConfig.foreignPillageFreeCleanTimes;
        this.lab_1.text = ""+this.curConfig.maxForeignDateTmes;
        this.lab_2.text = ""+this.curConfig.maxAttentionNum;
        this.lab_3.text = ""+this.curConfig.maxAttentionDateTimes;
        this.lab_4.text = ""+this.curConfig.maxAttentionPillageTimes;
        this.lab_5.text = ""+this.curConfig.washTimes;
        this.img_no_mean.visible = this.curConfig.level != 0;
        this.group_vip_1.text = this.curConfig.level+"";
    }

    private onClose(evt:egret.TouchEvent){
        if( evt )
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    private onAddBtn(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.onClose(null);
        BuyDiamondPanel.getInstance().show();
    }
    private onLeft(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( this.curPage == 0) return;
        this.curPage--;
        this.updateDisplay();
    }
    private onRight(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( this.curPage == this.maxPage-1) return;
        this.curPage++;
        this.updateDisplay();
    }

    private onAdded(evt:egret.Event){
        this.curPage = Model.me.vipLevel;
        this.updateDisplay();
        RedPointMgr.getInstance().update(RedPointMgr.VIP_MISSION,false);
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_add.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onAddBtn, this);
        this.img_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_add.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onAddBtn, this);
        this.img_left.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onLeft, this);
        this.img_right.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRight, this);
    }

    public show(){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
        if( ActivityMgr.getInstance().vipInfo){
            this.updateScoreRelated();
        }else{
            window["__vipInfo"]();
        }
    }

    public updateScoreRelated(){
        var info = ActivityMgr.getInstance().vipInfo;
        /**
         * TYPE_ÿ�յ�½ = 1;
         * TYPE_������½ = 2;
         * TYPE_�Ӷ���ʤ = 3;
         * STATUS_δ���� = 0;
         * STATUS_�Ѵ��� = 1;
         * STATUS_����ȡ = 2;
         **/
        var arr = [{type:1,status:info.dailyLoginStatus},{type:2,status:info.successionLoginStatus},{type:3,status:info.pillageWinStatus}];
        this.missionProvider.source = arr;

        if( Model.me.vipLevel+1 < ActivityMgr.getInstance().vipConfigs.length){
            var currentConfig:VipConfig = ActivityMgr.getInstance().vipConfigs[Model.me.vipLevel+1];
            if( currentConfig){
                this.group_score_0.text = Model.me.vipScore+"p"+currentConfig.score;
                if( Model.me.vipScore <= currentConfig.score){
                    this.img_pro_0.width = 175 * Model.me.vipScore/currentConfig.score;
                }else{
                    this.img_pro_0.width = 175;
                }
            }
        }
        this.group_vip_0.text = Model.me.vipLevel+"";
    }

    public img_pro_0:eui.Image;
    private mission_list:eui.List;
    public group_vip_0:eui.BitmapLabel;
    public group_score_0:eui.BitmapLabel;
    public group_vip_1:eui.BitmapLabel;
    public lab_0:eui.Label;
    public lab_1:eui.Label;
    public lab_2:eui.Label;
    public lab_3:eui.Label;
    public lab_4:eui.Label;
    public lab_5:eui.Label;
    public img_close:eui.Image;
    public img_add:eui.Image;
    public img_left:eui.Image;
    public img_right:eui.Image;
    public img_no_mean:eui.Image;

    private missionProvider:eui.ArrayCollection;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if( name == "img_pro_0"){
            this.img_pro_0 = instance;
        }else if( name == "mission_list"){
            this.mission_list = instance;
            this.mission_list.itemRenderer = ScoreItemRender;
            this.missionProvider = new eui.ArrayCollection();
            this.mission_list.dataProvider = this.missionProvider;
        }else if( name == "group_vip_0"){
            this.group_vip_0 = instance;
        }else if( name == "group_score_0"){
            this.group_score_0 = instance;
            this.group_score_0.textAlign = "center";
        }else if( name == "group_vip_1"){
            this.group_vip_1 = instance;
        }else if( name == "lab_0"){
            this.lab_0 = instance;
        }else if( name == "lab_1"){
            this.lab_1 = instance;
        }else if( name == "lab_2"){
            this.lab_2 = instance;
        }else if( name == "lab_3"){
            this.lab_3 = instance;
        }else if( name == "lab_4"){
            this.lab_4 = instance;
        }else if( name == "lab_5"){
            this.lab_5 = instance;
        }else if( name == "img_close"){
            this.img_close = instance;
        }else if( name == "img_add"){
            this.img_add = instance;
        }else if( name == "img_left"){
            this.img_left = instance;
        }else if( name == "img_right"){
            this.img_right = instance;
        }else if( name == "img_no_mean"){
            this.img_no_mean = instance;
        }
    }
    //public childrenCreated(){
    //    super.childrenCreated();
        //if( !this.flag){
        //    this.updateDisplay();
        //    this.flag = true;
        //}
    //}
    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    private static _instance:VipPanel;
    public static getInstance():VipPanel{
        if( VipPanel._instance == null)
            VipPanel._instance = new VipPanel();
        return VipPanel._instance;
    }
}